<template>
<view>
<form-detail-component id="selectAddress" showSubmitPopup="false" :userAddressCustomFormCommitId="curCommitformId" showBtn="true" :receiveData="sendOptionData" :locationList2="locationList" v-if="sendPageDataState" @selectPsotion="selectPsotion" @nextStep="nextStepFun" @perStep="perStepFun"></form-detail-component>
<!-- <view class='shareFormPoster' style='background:{{setting.platformSetting.defaultColor}}' bindtap='showPoster'>
  <image class='shareFormPosterImg' src='https://image1.sansancloud.com/xianhua/2018_12/26/11/57/10_263.jpg'></image>
</view> -->
<form-chain-poster-component :formId="formId" :ewmImgUrl="ewmImgUrl" v-if="posterState" @closePoaster="getChilrenPoster"></form-chain-poster-component>
 <!-- 首页组件-详情装饰-->
 <custom-page-index-component :receiveData="sendFormData" v-if="sendFormData"></custom-page-index-component>
</view>
</template>

<script>

export default {
  data() {
    return {
      formChainData: null,
      curCommitformId: '',
      formData: null,
      processType: false,
      refProductFormType: false,
      productId: 0,
      formId: 0,
      chainId: 0,
      reqLocation: false,
      locationList: {},
      locationIndex: "",
      sendOptionData: null,
      sendPageDataState: false,
      sendFormData: null,
      params: {
        formJson: '',
        customFormId: '',
        miniNotifyFormId: ''
      },
      posterState: false,
      ewmImgUrl: "",
      setting: "",
      loginUser: ""
    };
  },

  props: {},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    console.log(options);
    let sendFormData = JSON.stringify({
      title: 'noTitle',
      url: "form_chain_" + options.chainId
    });
    that.setData({
      sendFormData: sendFormData
    });
    that.gainActionEvent = options.actionEvent;
    that.setData({
      chainId: options.chainId
    });
    that.params.chainId = options.chainId;

    if (options && options.actionEvent) {
      that.processType = true;
    } else {
      that.processType = false;
    }

    if (options && options.productId) {
      that.refProductFormType = true;
      that.productId = options.productId;

      if (options.params) {
        that.skuData = JSON.parse(options.params);
      } else {
        that.skuData = that.skuData;
      }
    } else {
      that.refProductFormType = false;
    }

    if (options.chainInstanceId) {
      that.params.chainInstanceId = options.chainInstanceId;
    }

    if (options.chainId) {
      that.params.chainId = options.chainId;
    }

    that.getFormChainDataFun(options, 'init');
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.setData({
      setting: getApp().globalData.setting
    });
    this.setData({
      loginUser: getApp().globalData.loginUser
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this;

    if (that.reqLocation) {
      // that.setData({ sendPageDataState: false })
      // that.setData({ sendPageDataState: true })
      let locationList = {};
      console.log("从选择地点页面返回", that.selectAddress);
      var pages = getCurrentPages();
      var currPage = pages[pages.length - 1]; //当前页面

      console.log(currPage); //就可以看到data里mydata的值了

      if (that.selectAddress) {
        console.log("选择了地点");
        console.log("locationIndex", that.locationIndex);
        locationList[that.locationIndex] = that.selectAddress;
        that.locationList = Object.assign({}, that.locationList, locationList);
        that.setData({
          locationList: that.locationList
        });
        console.log("==locationList==", locationList);
        console.log("==that.data.locationList ==", that.locationList);
      } else {
        console.log("没选择地点");
      }

      that.selectComponent("#selectAddress").selectAddress();
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},
  methods: {
    // 关闭海报
    getChilrenPoster(e) {
      let that = this;
      that.setData({
        posterState: false
      });
    },

    showPoster: function () {
      let that = this;
      console.log('===showPoster====', that.params.customFormId);
      let ewmImgUrl = getApp().globalData.getQrCode({
        type: "form_detail",
        id: that.params.customFormId
      });
      that.setData({
        posterState: true,
        ewmImgUrl: ewmImgUrl
      });
    },
    tolinkUrl: function (e) {
      if (!getApp().globalData.loginUser) {
        wx.showModal({
          title: '提示',
          content: '主人~您还在登陆哦!稍等片刻',
          success: function (res) {
            if (res.confirm) {
              console.log('用户点击确定');
            } else if (res.cancel) {
              console.log('用户点击取消');
            }
          }
        });
        return;
      }

      let that = this;
      let linkUrl = e.currentTarget ? e.currentTarget.dataset.link : e;
      let index = e.currentTarget ? e.currentTarget.dataset.index : 0;

      if (linkUrl.indexOf("select_location.html") != -1) {
        console.log("选择位置");
        that.setData({
          reqLocation: true,
          locationIndex: "position_" + index
        });
      } else {
        that.setData({
          reqLocation: false
        });
      }

      getApp().globalData.linkEvent(linkUrl);
    },
    selectPsotion: function (e) {
      let that = this;
      console.log("===selectPsotion===", e);
      let locationIndex = e.detail.locationIndex;
      that.setData({
        reqLocation: true,
        locationIndex: locationIndex
      });
    },
    getFormChainDataFun: function (options, reqType, index, callback) {
      let that = this;
      console.log("===getFormChainDataFun===", options);
      let params = {
        chainId: options.chainId || 0,
        chainInstanceId: options.chainInstanceId || 0
      };
      let formChainDataUrl = getApp().globalData.AddClientUrl("/wx_create_new_or_get_custom_form_chain_instance.html", params, 'get');
      wx.request({
        url: formChainDataUrl.url,
        data: formChainDataUrl.params,
        header: getApp().globalData.headerPost,
        method: 'get',
        success: function (res) {
          console.log(res);
          let formChainData = res.data.relateObj;

          if (formChainData.customFormChainBean && formChainData.customFormChainBean.formsJson) {
            try {
              formChainData.customFormChainBean.formsJson = JSON.parse(formChainData.customFormChainBean.formsJson);
            } catch (e) {
              formChainData.customFormChainBean.formsJson = formChainData.customFormChainBean.formsJson;
              console.log(e);
            }
          }

          console.log("formChainData", formChainData);
          that.setData({
            formChainData: formChainData
          });

          if (formChainData.customFormChainBean.formsJson.forms && formChainData.customFormChainBean.formsJson.forms.length > 0) {
            let initIndex = 0;

            if (index) {
              initIndex = index;
            }

            let formId = formChainData.customFormChainBean.formsJson.forms[initIndex].formId;
            let totalLength = formChainData.customFormChainBean.formsJson.forms.length;
            let detailFormId = '';

            if (formChainData.commits[initIndex]) {
              console.log("===initIndex===", formChainData.commits[initIndex]);
              detailFormId = formChainData.commits[initIndex].id;
            }

            that.getSomeFormData(formId, initIndex, totalLength, formChainData.id, detailFormId);
          }

          if (reqType == 'init') {
            wx.setNavigationBarTitle({
              title: that.formChainData.chainName
            });
          }

          if (callback) {
            callback(formChainData);
          }
        }
      });
    },
    perStepFun: function (data) {
      console.log("===perStepFun===", data);
      let that = this;
      let perStepIndex = data.detail.perStepIndex;
      let formChainData = that.formChainData;
      let params = {
        chainId: that.params.chainId || 0,
        chainInstanceId: formChainData.id || 0
      };
      let formId = formChainData.customFormChainBean.formsJson.forms[perStepIndex].formId;
      let totalLength = formChainData.customFormChainBean.formsJson.forms.length;
      that.getFormChainDataFun(params, 'next', perStepIndex);
      console.log("===perStepIndex===", perStepIndex);
    },
    nextStepFun: function (data) {
      console.log("===nextStepFun===", data);
      let that = this;
      let nextStepIndex = data.detail.nextStepIndex;
      let formChainData = that.formChainData;
      console.log("===nextStepIndex===", nextStepIndex);
      let totalLength = formChainData.customFormChainBean.formsJson.forms.length;
      let params = {
        chainId: that.params.chainId || 0,
        chainInstanceId: formChainData.id || 0
      };

      if (nextStepIndex < totalLength) {
        let formId = formChainData.customFormChainBean.formsJson.forms[nextStepIndex].formId;
        that.getFormChainDataFun(params, 'next', nextStepIndex);
      } else {
        console.log("已提交完所有数据");
        that.tolinkUrl("form_chain_success.html");
      }
    },
    getSomeFormData: function (formId, curIndex, totalLength, chainInstanceId, detailFormId) {
      console.log("===getSomeFormData===", formId, detailFormId);
      let that = this;
      let sendOptionData = {
        customFormId: formId,
        formChainData: {
          curIndex: curIndex,
          totalLength: totalLength,
          chainInstanceId: chainInstanceId
        }
      };
      that.setData({
        curCommitformId: detailFormId,
        formId: formId,
        sendPageDataState: false,
        sendOptionData: sendOptionData,
      });
      setTimeout(function(){
          that.setData({sendPageDataState: true})
      },500)
    },
    setData: function (obj) {
      let that = this;
      let keys = [];
      let val, data;
      Object.keys(obj).forEach(function (key) {
        keys = key.split('.');
        val = obj[key];
        data = that.$data;
        keys.forEach(function (key2, index) {
          if (index + 1 == keys.length) {
            that.$set(data, key2, val);
          } else {
            if (!data[key2]) {
              that.$set(data, key2, {});
            }
          }

          data = data[key2];
        });
      });
    }
  }
};
</script>
<style>
@import "./index.css";
</style>